﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Module_Basic_Entities
{
    public class Layer2D : Layer
    {
        public Layer2D(List<List<Tile>> list, int mapWidth,
            int mapHeight, int tileWidth, int tileHeight, List<BLKSTR> blocks)
            : base(list, mapWidth,
                mapHeight, blocks)
        {
            this.TileWidth = tileWidth;
            this.TileHeight = tileHeight;
        }


        public int TileWidth
        {
            get;
            set;
        }

        public int TileHeight
        {
            get;
            set;
        }

        public List<Tile> IntersectTiles(Rectangle objDestRect)
        {
            var tiles = from ts in this.Tiles
                        from t in ts
                        where Tile.Intersects(objDestRect, t)
                        select t;

            if (tiles.Count() == 0)
                return null;

            return tiles.ToList();
        }

        public Tile? ContainsTile(Vector2 pos)
        {
            var tiles = from ts in this.Tiles
                        from t in ts
                        where Tile.Contains(pos, t)
                        select t;

            if (tiles.Count() == 0)
                return null;

            return tiles.First();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="gameTime"></param>
        public void Draw(float gameTime, SpriteBatch spriteBatch, Vector2 worldPosition, Color color)
        {
            foreach (var rowOftiles in this.Tiles)
            {
                foreach (var tile in rowOftiles)
                {
                    spriteBatch.Draw(this.TileStripTexture, worldPosition + tile.Position,
                     this.GetTileSourceRectangle(tile.Id), color);
                }
            }
        }
    }
}
